const app = getApp()
var isLogin = false
const toast = require('../../utils/toast.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nameTitle: '房东昵称',
    introduction: '',
    isCollected: false
  },
  toast: toast,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('房东id : ' + options.sliId)
    this.getIntentData(options)
    this.connectNetForData()
  },
  /**
   * 连接网络请求数据
   */
  connectNetForData() {
    var that = this
    wx.request({
      url: app.baseNet + 'LandlordInfo/querylandlordInfoById',
      data: {
        sliId: this.data.sliId,
        username: this.data.username
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.statusCode == 200) {
          var dd = res.data
          var land = dd.landlordInfo
          if (dd.messcode == '001') {
            that.setData({
              iconName: land.portraitSrc,
              nameTitle: land.nickname,
              types: land.type,
              introduction: land.introduction,
              list: dd.roomInfoList
            })
          }
        }
      }
    })
  },
  /**
   * 传递参数
   */
  getIntentData(options) {
    var phone = this.toLogin()
    var sliId = options.sliId
    this.setData({
      sliId: sliId,
      username: phone
    })
  },
  toLogin() {
    var phoneNumber = wx.getStorageSync('phoneNumber')
    if (typeof phoneNumber == 'undefined' || phoneNumber == '' || phoneNumber == null) {
      phoneNumber = ''
      isLogin = true
    } else {
      isLogin = false
    }
    return phoneNumber
  },

  /**
   * 收藏
   */
  collectClick: function(e) {
    var index = e.currentTarget.dataset.id
    console.log(':::::' + index)
    if (isLogin) { //跳转登录界面
      wx.navigateTo({
        url: '../loginAndRegister/loginAndRegister?jumpType=' + 3,
      })
    } else {
      wx.showLoading({
        title: '加载中...'
      })
      this.collectionSet(index)
    }
  },
  /**
   * 收藏
   */
  collectionSet: function(index) {
    var that = this
    var srId = this.data.list[index].sriId
    var isColl = this.data.list[index].falg
    wx.request({
      url: app.baseNet + 'roomCollection/insertRoomCollection',
      data: {
        srId: srId,
        username: this.data.username
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.statusCode == 200) {
          var iscollected = 'list[' + index + '].falg'
          if (isColl == 1) { //取消收藏
            that.setData({
              [iscollected]: 2
            })
          } else { //收藏
            that.setData({
              [iscollected]: 1
            })
          }
        } else {
          toast.showSystemAbnormalToast()
        }
        wx.hideLoading()
      }

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var phone = this.toLogin()
    this.setData({
      username: phone
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})